/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.StudyCardEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class StudyCardDao extends CommonDao {

    private static final String TABLE_NAME = "studycard";

    public StudyCardDao() {
        super(StudyCardDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        StudyCardEntity entity = new StudyCardEntity();
        try {
            entity.setId(resultSet.getInt("ID"));
            entity.setDescription(resultSet.getString("DESCRIPTION"));
            entity.setLink(resultSet.getString("LINK"));
            entity.setPicture(resultSet.getString("PICTURE"));
            entity.setPrice(resultSet.getFloat("PRICE"));
            entity.setSchoolId(resultSet.getInt("SCHOOL_ID"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return entity;
    }

    public int add(StudyCardEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + "(`LINK`,`DESCRIPTION`,`PICTURE`,`PRICE`,`SCHOOL_ID`) "
                + "values ("
                + "'" + entity.getLink() + "',"
                + "'" + entity.getDescription() + "',"
                + "'" + entity.getPicture() + "',"
                + entity.getPrice() + ","
                + entity.getSchoolId()
                + ")";
        return super.insert(sql);
    }

    public boolean update(StudyCardEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`LINK` = '" + entity.getLink() + "',"
                + "`DESCRIPTION` = '" + entity.getDescription() + "',"
                + "`PICTURE` = '" + entity.getPicture() + "',"
                + "`PRICE` = " + entity.getPrice() + ","
                + " `SCHOOL_ID` = " + entity.getSchoolId()
                + " where id = " + entity.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id = " + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public StudyCardEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<StudyCardEntity> entitys = (List<StudyCardEntity>) super.select(sql);
        return entitys.get(0);
    }

    public List<StudyCardEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<StudyCardEntity>) super.select(sql);
    }
}
